package thss.mse12.lau.rsa.utils;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

public class GUI extends JFrame {
	Container c;

	public thss.mse12.lau.rsa.a.RSA aRSA;

	public thss.mse12.lau.rsa.b.RSA bRSA;

	public byte[] encryptedData1, encryptedData2;

	public JLabel label1, label2, label3, label4, label5, label6, label7;

	public JButton button1, button2;

	public JTextArea ta1, ta2, ta3, ta4;

	public Font cn_font = new Font("微软雅黑", Font.PLAIN, 13);
	public Font en_font = new Font("方正舒体", Font.PLAIN, 13);
	public Border blackBorder = BorderFactory.createLineBorder(Color.black);

	public GUI() {
		super("RSA");
		aRSA = new thss.mse12.lau.rsa.a.RSA();
		bRSA = new thss.mse12.lau.rsa.b.RSA();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(800, 600);

		c = this.getContentPane();
		c.setLayout(null);

		JPanel panel1 = new JPanel();
		panel1.setBorder(blackBorder);
		panel1.setBounds(20, 20, 760, 240);
		panel1.setLayout(null);
		c.add(panel1);

		JPanel panel2 = new JPanel();
		panel2.setBorder(blackBorder);
		panel2.setBounds(20, 280, 240, 280);
		panel2.setLayout(null);
		c.add(panel2);

		JPanel panel3 = new JPanel();
		panel3.setBorder(blackBorder);
		panel3.setBounds(280, 280, 240, 280);
		panel3.setLayout(null);
		c.add(panel3);

		JPanel panel4 = new JPanel();
		panel4.setBorder(blackBorder);
		panel4.setBounds(540, 280, 240, 280);
		panel4.setLayout(null);
		c.add(panel4);

		label1 = new JLabel("请输入欲加密文字");
		label1.setFont(cn_font);
		label1.setBounds(25, 5, 130, 15);

		ta1 = new JTextArea();
		ta1.setBounds(25, 25, 710, 160);
		ta1.setFont(cn_font);
		ta1.setLineWrap(true);

		button1 = new JButton("加密");
		button1.setFont(cn_font);
		button1.setBounds(540, 190, 80, 30);

		button1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String source = ta1.getText();
				byte[] data = source.getBytes();

				try {
					long time1 = System.currentTimeMillis();
					encryptedData1 = aRSA.encrypt(data);
					long time2 = System.currentTimeMillis();
					label5.setText("加密耗时：" + (time2 - time1) + " ms");
					ta2.setText(new String(encryptedData1));

					long time3 = System.currentTimeMillis();
					encryptedData2 = bRSA.encrypt(data);
					long time4 = System.currentTimeMillis();
					label6.setText("加密耗时：" + (time4 - time3) + " ms");
					ta3.setText(new String(encryptedData2));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});

		button2 = new JButton("解密");
		button2.setFont(cn_font);
		button2.setBounds(640, 190, 80, 30);

		button2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				byte[] decryptedData1, decryptedData2;
				try {
					long time1 = System.currentTimeMillis();
					decryptedData1 = aRSA.decrypt(encryptedData1);
					long time2 = System.currentTimeMillis();
					label5.setText("解密耗时：" + (time2 - time1) + " ms");
					ta2.setText(new String(decryptedData1));

					long time3 = System.currentTimeMillis();
					decryptedData2 = bRSA.decrypt(encryptedData2);
					long time4 = System.currentTimeMillis();
					label6.setText("解密耗时：" + (time4 - time3) + " ms");
					ta3.setText(new String(decryptedData2));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});

		panel1.add(ta1);
		panel1.add(button1);
		panel1.add(button2);
		panel1.add(label1);

		label2 = new JLabel("使用Java自带Cipher加密");
		label2.setFont(cn_font);
		label2.setBounds(10, 5, 160, 15);

		ta2 = new JTextArea();
		ta2.setFont(cn_font);
		ta2.setBounds(10, 25, 220, 200);
		ta2.setEditable(false);
		ta2.setLineWrap(true);

		panel2.add(label2);
		panel2.add(ta2);

		label3 = new JLabel("使用自己实现之RSA加密");
		label3.setFont(cn_font);
		label3.setBounds(10, 5, 160, 15);

		ta3 = new JTextArea();
		ta3.setFont(cn_font);
		ta3.setBounds(10, 25, 220, 200);
		ta3.setEditable(false);
		ta3.setLineWrap(true);

		panel3.add(label3);
		panel3.add(ta3);

		label4 = new JLabel("使用自己实现之BigInteger+RSA加密");
		label4.setFont(cn_font);
		label4.setBounds(10, 5, 230, 15);

		panel4.add(label4);

		label5 = new JLabel("加密耗时：");
		label5.setFont(cn_font);
		label5.setBounds(10, 230, 160, 15);

		label6 = new JLabel("加密耗时：");
		label6.setFont(cn_font);
		label6.setBounds(10, 230, 160, 15);

		panel2.add(label5);
		panel3.add(label6);

		label7 = new JLabel("此部分仍在施工中");
		label7.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		label7.setBounds(25, 100, 200, 20);

		panel4.add(label7);
		setVisible(true);
		setResizable(false);
	}

	public static void main(String[] args) {
		GUI gui = new GUI();

	}
}
